// https://leetcode-cn.com/problems/validate-binary-search-tree/
// 98. 验证二叉搜索树
// 给定一个二叉树的根节点 root，判断其是否一个有效的二叉搜索树
import { TreeNode } from '../utils/index.ts';

function isValidBST(root: TreeNode | null): boolean {
    function helper(
        root: TreeNode | null,
        lower: number,
        upper: number
    ): boolean {
        if (root === null) return true;
        if (root.val <= lower || root.val >= upper) return false;

        return (
            helper(root.left, lower, root.val) &&
            helper(root.right, root.val, upper)
        );
    }

    return helper(root, -Infinity, Infinity);
}
